package com.sllp.C71S3TsllpShop.web.back;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.ChartData;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.biz.impl.BusinessBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersItemBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.ProductBizImpl;
import com.sllp.C71S3TsllpShop.util.GenerateOrdersItemChartData;

@Controller
public class BackFinancialChartAction {

	@Resource
	private BusinessBizImpl bbi;
	@Resource
	private ProductBizImpl pbi;
	@Resource
	private OrdersItemBizImpl oibi;
	@Resource
	private GenerateOrdersItemChartData generateOrdersItemChartData;
	
	@GetMapping("back/toFinancialChart.do")
	public String toFinancialChart() {
		return "back/financial-chart::financialChart";
	}
	
	@PostMapping("back/toShowChart.do")
	public String toShowChart(@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			Product product,Model m) {
		product.setBusiness(business.getId() );
		if(product.getName() !=null && product.getName() =="") {
			product.setName(null);
		}
		if(product.getName() != null && product.getName() != "") {
			product.setName("%"+product.getName()+"%");
		}
		OrdersItem ordersItem1 =  new OrdersItem();
		List<OrdersItem> list = oibi.selectOrdersItem(product, null, null,ordersItem1 , null, null);
		List<OrdersItem> ordersItemList = new ArrayList<OrdersItem>();
		// 已结束的订单
		for(OrdersItem ordersItem : list) {
			System.out.println(ordersItem.getId()+"=="+ordersItem.getState());
			if(ordersItem.getState() == 4 || ordersItem.getState() == 7 || ordersItem.getState() == 8) {
				ordersItemList.add(ordersItem);
			}
		}
		List<ChartData> chartData = generateOrdersItemChartData.generateOrdersItemChartData(ordersItemList);
		System.out.println(list.size());
		System.out.println(chartData.size());
		System.out.println(ordersItemList.size());
		m.addAttribute("indexChartData",chartData);
		m.addAttribute("ordersItemList", ordersItemList);
		return "back/financial-chart::showChart";
	}
	
}
